import { _decorator, Component, instantiate, Label, Node, Prefab, TextAsset, tween, UITransform, v3 } from 'cc';
import { UIComponent } from '../../../../Framework/Scripts/Managers/UIComponent';
import { ExcelMgr } from '../../../../Framework/Scripts/Managers/ExcelMgr';
import { ResManager } from '../../../../Framework/Scripts/Managers/ResManager';
import { CocosUtils } from '../../../../Framework/Scripts/Utils/CocosUtils';
import { AbNames } from '../../Config/ResConfig';
import { Block } from './Block';
const { ccclass, property } = _decorator;

@ccclass('GamePlayMap')
export class GamePlayMap extends UIComponent {
    private _gameType: number = -1;//游戏类型 枚举GameType 
    set gameType(v){
        this._gameType = v;
    }

    lab1: Label; //第几题
    labDes: Label; //题目
    blockParent: Node;

    tid: number; //当前题目id
    protected onLoad(): void {
        this.InitUI();
        this.InitData();
    }

    start() {
     
    }

    protected onDestroy(): void {
        this.unRegisterEvent();
    }

    private InitUI(){
        this.registerEvent();

        this.lab1 = this.ViewComponent('view/content/spBg/node1/lab1',Label);
        this.labDes = this.ViewComponent('view/content/spBg/node2/lab1',Label);
        this.blockParent = this.ViewNode('view/content/blockParent');
    }

    private InitData(){
        this.tid = -1;
    }

    private registerEvent(){
        // director.on(EventMsgKes.loading,this.onLoadingPro,this);
    }

    private unRegisterEvent(){
        // director.off(EventMsgKes.loading,this.onLoadingPro,this);
    }

    //展示题目
    public showTopic(time: number,tid: number,tidx: number){
        console.log('==========showTopic============')
        this.lab1.string = `第${tidx+1}题`;
        this.tid  = tid;
        let topicId = tid;
        
        if(topicId){
            let txt = ResManager.inst.getAsset(AbNames.GamePlay,'Excle/DT',TextAsset) as TextAsset;
            ExcelMgr.inst.AddTable('DT',txt.text);

            let res = ExcelMgr.inst.QueryOne('DT',topicId);
            console.log('=========res============',res);

            let topic = res['Topic'];
            let topicChoiceCount = res['TopicChoiceCount'];

            if(topic){
                this.labDes.string = topic;
            }

            this.blockParent.removeAllChildren();

            let offsetX = 30;
            let pre = ResManager.inst.getAsset(AbNames.GamePlay,'Prefabs/GamePlay/Block',Prefab);
            //获得实际的尺寸
            let winSize = CocosUtils.winSize();
            console.log('======winSize2=======',winSize);
            for (let i = 0; i < topicChoiceCount; i++) {
                let item:any = instantiate(pre); 
                this.blockParent.addChild(item);
                let xx = winSize.width/2 + item.getComponent(UITransform).width/2;
                let yy = -item.getComponent(UITransform).height/2 - i*(item.getComponent(UITransform).height + offsetX);
                item.setPosition(v3(xx,yy,0));

                let ts_node:Block = item.addComponent(Block);
                ts_node.gameType = this._gameType;
                let str = res[`TopicChoice${i+1}`]
                ts_node.setData(i,str,topicId);
          
                //播放动画
                tween()
                    .target(item)
                    .delay(0.15*(i+1))
                    .to(0.5,{position:v3(0,item.position.y, 0)}, { easing: 'elasticInOut' })
                    .start()
            }
        }
    }

    // 展示题目主题赛记录
    public showTopicRecord(tid: number,idx:number){
        if(tid){
            let txt = ResManager.inst.getAsset(AbNames.GamePlay,'Excle/DT',TextAsset) as TextAsset;
            ExcelMgr.inst.AddTable('DT',txt.text);

            let res = ExcelMgr.inst.QueryOne('DT',tid);
            console.log('=========res============',res);

            let topic = res['Topic'];
            let topicChoiceCount = res['TopicChoiceCount'];
            if(topic){
                this.labDes.string = topic;
            }

            this.lab1.string = `第${idx+1}题`

            this.blockParent.removeAllChildren();
            let offsetX = 30;
            let pre = ResManager.inst.getAsset(AbNames.GamePlay,'Prefabs/GamePlay/Block',Prefab) as Prefab;
          
            //获得实际的尺寸
            let winSize = CocosUtils.winSize();
            console.log('======winSize2=======',winSize);
            for (let i = 0; i < topicChoiceCount; i++) {
                let item = instantiate(pre); 
                this.blockParent.addChild(item);
                let xx = 0;
                let yy = -item.getComponent(UITransform).height/2 - i*(item.getComponent(UITransform).height + offsetX);
                item.setPosition(v3(xx,yy,0));
                // console.log('=====Block1=========',i);
                let ts_node:Block = item.addComponent(Block);
                let str = res[`TopicChoice${i+1}`]
                // console.log('=====Block2=========',i);
                ts_node.setData(i,str,tid);
                ts_node.setBtnStatus(false);
            }
        }
    }

    /**
     * 
     * @param choiceId 选择的答案索引
     * @param isDui 是否答对
     * @param isSelf 是否是自己
     */
    public setChoicesData(choiceId: number, isDui: boolean, isSelf: boolean) {
        let children = this.blockParent.children;
        for (let i = 0; i < children.length; i++) {
            const block = children[i];
            let ts_block = block.getComponent(Block);
            if (ts_block.cid === choiceId) {
                ts_block.setMatchGameData(isDui, isSelf)
                break;
            }
        }
    }

    public setAllUnClick(){
        let children = this.blockParent.children;
        console.log('========setAllUnClick==============',children.length);
        for (let i = 0; i < children.length; i++) {
            const block = children[i];
            let ts_block = block.getComponent(Block);
            ts_block.setBtnStatus(false);
        }
    }
    
    //当前题目答完毕，设置正确的答案
    public setCurrentChoice(cid: number) {
        console.log('============onGameAnswerEndBroadcast============', cid)
        let children = this.blockParent.children;
        for (let i = 0; i < children.length; i++) {
            const block = children[i];
            let ts_block = block.getComponent(Block);
            if (ts_block.cid === cid) {
                ts_block.setMatchGameData2();
                break;
            }
        }
    }
}


